<template lang="pug">
  .mask(v-show='isShowu')
    .wrap
      .w-title
        .wt-add 添加用户
        .wt-close(@click="cancel()") ×
      .addcompany
        el-form(:model="ruleForm" :label-position="labelPosition" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm")
          el-form-item(label="用户账户" prop="cpanycode")
            el-input(v-model="ruleForm.cpanycode" placeholder="请输入用户账号")
          el-form-item(label="用户名称" prop="cpanyname")
            el-input(v-model="ruleForm.cpanyname" placeholder="请输入用户名称")
          el-form-item(label="用户角色" prop="level")
            el-select(v-model="ruleForm.level" placeholder="请选择用户角色")
              el-option(label="超级管理员" :value=1)
              el-option(label="系统管理员" :value=2)
              el-option(label="普通用户" :value=3)
          el-form-item(label="密码" prop="regioncode" )
            el-input(v-model="ruleForm.regioncode"  type="password" placeholder="请输入密码")
          el-form-item(label="确认密码" prop="regioncode1")
            el-input(v-model="ruleForm.regioncode1"  type="password" placeholder="请再次输入密码")
          <!--el-form-item(label="单位联系人" prop="copaypersonal")-->
          <!--el-input(v-model="ruleForm.copaypersonal")-->
          <!--el-form-item(label="联系方式" prop="cpanyphone")-->
          <!--el-input(v-model="ruleForm.cpanyphone")-->
          <!--el-form-item(label="单位名称" prop="unitarycode")-->
          <!--el-select(v-model="ruleForm.unitarycode" filterable placeholder="请选择")-->
          <!--el-option(-->
          <!--v-for="item in ruleForm.options"-->
          <!--:key="item.organizationCode"-->
          <!--:label="item.organizationName"-->
          <!--:value="item.organizationCode")-->
        .btns
          el-button(type="danger" @click="submitForm('ruleForm')" ) 添加
          el-button(@click="cancel()") 取消
        //- div {{canpaylist}}{{isupdate}}
</template>
<script>
  import {mapGetters, mapActions} from 'vuex'
  // let options =  [{
  //           value: 'a',
  //           label: '黄金糕'
  //         }, {
  //           value: 'a',
  //           label: '双皮奶'
  //         }, {
  //           value: 'a',
  //           label: '蚵仔煎'
  //         }, {
  //           value: 'a',
  //           label: '龙须面'
  //         }, {
  //           value: 'a',
  //           label: '北京烤鸭'
  //         }]
  export default {
    name: 'adduser',
    props: ['user','code'],
    data() {
      var validateuser1 = (rule, value, callback) => {
        var arr = value.split('')
        var reg = /^[\-0-9a-zA_Z]+$/;
        for (let i = 0; i < arr.length; i++) {
          if (!reg.test(arr[i])) {
            callback(new Error('账号必须为英文或数字或下划线'));
          }
        }
        callback();
      }
      var validateuser2 = (rule, value, callback) => {
        var arr = value.split('')
        var reg = /^[\u4E00-\u9FA5A-Za-z0-9]+$/;
        for (let i = 0; i < arr.length; i++) {
          if (!reg.test(arr[i])) {
            callback(new Error('用户名必须为中文、英文和数字'));
          }
        }
        callback();
      }
      return {
        organizationCode:this.code,
        isShowu: this.user,
        labelPosition: 'right',
        industrylist: [],
        ruleForm: {
          cpanycode: '',
          cpanyname: '',
          level: '',
          regioncode: '',
          regioncode1: '',
          //copaypersonal:'',
          //cpanyphone:'',
          options: [],
          //unitarycode:''
        },
        list: {},
        rules: {
          cpanycode: [
            {required: true, message: '请输入用户账户', trigger: 'blur'},
            {validator: validateuser1, trigger: 'blur'}
          ],
          cpanyname: [
            {required: true, message: '请输入用户名称', trigger: 'blur'},
            {validator: validateuser2, trigger: 'blur'}
          ],
          regioncode: [
            {required: true, message: '请输入密码', trigger: 'blur'},
            {min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur'},
          ],
          regioncode1: [
            {required: true, message: '请再一次输入密码', trigger: 'blur'},
            {min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur'},
          ],
          level: [
            {type: 'number', required: true, message: '请选择角色分类', trigger: 'change'}
          ],
//        copaypersonal: [
//          { required: true, message: '请输入单位联系人', trigger: 'blur' },
//        ],
//        cpanyphone: [
//          { required: true, message: '请输入联系方式', trigger: 'blur' },
//        ],
//        unitarycode:[
//          { required: true, message: '请选择单位名称', trigger: 'change' }
//        ],
        }
      }
    },
    mounted() {
      this.getunitray()
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.addlist()
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      async getunitray() {
        let res = await this.$ajax('xyssoManager/corp/queryCorp')
        this.ruleForm.options = res.data

      },
      async addlist() {
        if (this.ruleForm.regioncode != this.ruleForm.regioncode1) {
          this.$message.error('两次密码不一样')
          return
        }
        let list = {
          account: this.ruleForm.cpanycode,
          userName: this.ruleForm.cpanyname,
          role: this.ruleForm.level,
          password: this.ruleForm.regioncode,
          organizationCode:this.organizationCode
          //contacts: this.ruleForm.copaypersonal
        }
        let res = await this.$ajax.post('xyssoManager/sysuser/insert', list)
        let data = res.data
        if (data.code == 200) {
          this.$message.success('添加成功')
          this.isShowu=false
          this.$emit("refreshList")
          this.$router.push('unitmanagement')
        }
      },
      cancel() {
        this.isShowu = false
      },
    },
    watch:{
      user(cur){
        if(cur==true){
          this.isShowu=cur
        }
      },
      code(cur){
          this.organizationCode=cur
      }
    }
  }
</script>
<style lang="stylus" scoped>
  .mask
  //display none
    position fixed
    top 0
    left 0
    right 0
    bottom 0
    background rgba(0, 0, 0, 0.5)
    .wrap
      position absolute
      top 50%
      left 50%
      margin-left -200px
      margin-top -226px
      width 400px
      height 452px
      background white
      border-radius 5px
      .w-title
        height 50px
        line-height 50px
        border-bottom 1px solid #eaeaea
        color #333333
        .wt-add
          float left
          font-size 16px
          margin-left 20px
        .wt-close
          float right
          margin-right 20px
          font-size 36px
          color #9d9d9d
          cursor pointer
      .addcompany
        padding 30px 0 0
        .demo-ruleForm
          margin 0 auto
          width 280px
          .el-input
          .el-select
            width 100%
            font-size 12px
        .btns
          margin-top 10px
          text-align center
          button
            width 135px

</style>


